
import 'dart:async';
import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/AppData.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class FixPasswordController extends BaseController{


  String phoneNumber = AppData.phoneNumber!;

  String encodePhoneNumber = Tools.encryptPhoneNumber(AppData.phoneNumber!);

  //新密码输入框
  TextEditingController passwordTxt = TextEditingController()..clear();

  //新密码确认框
  TextEditingController confirmPasswordTxt = TextEditingController()..clear();

  //验证码输入框
  TextEditingController vertifyTxtCtr = TextEditingController()..clear();


  int countdown = 0; //倒计时数字
  String countdownTxt = '获取验证码'; //倒计时的文本

  //类型 0 修改登录密码 1 修改支付密码
  int type = 0;

  ///页面标题
  String pageTitle = '';
  //当前步骤
  int step = 1;

  //验证码按钮颜色
  Color buttonColor =  ThemeConfig.themeColor;


  @override
  void onInit() {
    super.onInit();
    type=Get.arguments['type'];
  }

  //修改哪个密码
  void choise(int types){
    type = types;
    if(type==0){
      pageTitle = '修改登录密码';
    }else{
      pageTitle = '修改支付密码';
    }
    update();
    Get.toNamed(Routes.updatePassword);
  }



  Timer? timer;
  //接收旧手机号的验证码
  void sendVerfityCode() {
    
    print(countdown);
    if (countdown == 0) {
      countdown = 60;
      if(type==0){
        Network.getMsgCode(phoneNumber,'edit_password');
      }else{
        Network.getMsgCode(phoneNumber,'edit_pay_password');
      }
      
      timer = Timer.periodic(Duration(seconds: 1), (timer) {
        countdown--;
        countdownTxt = "重发(${countdown.toString()})";
        buttonColor = Tools.fromHex("#D7D7D7");
        if (countdown == 0) {
          countdownTxt = '重新发送';
          buttonColor = ThemeConfig.themeColor;
          timer.cancel();
        }
        update();
      });
      
          
    }
  }


  //下一步及提交
  void submit(){
    if(step==1){
      if(vertifyTxtCtr.text==''){
        Tools.showToast("请先输入验证码");
        return;
      }

      String msgType;
      if (type==0) {
        msgType = 'edit_password';
      }else{
        msgType = 'edit_pay_password';
      }
      //手机验证
      Network.checkMsgCode(phoneNumber, vertifyTxtCtr.text, msgType).then((value){
        if(value){
          vertifyTxtCtr.text = '';//重置验证码输入框
          step = 2;//进入步骤2
          if (timer != null) {
            timer!.cancel();
            countdown = 0;
            countdownTxt = "获取验证码";
            buttonColor = ThemeConfig.themeColor;
          }
          update();
        }
      });
    }else{

      if(passwordTxt.text==''){
        Tools.showToast("密码不能为空");
        return;
      }

      
      print("新密码提交");
      
      String confirmPassword;
      if(type==0){
         confirmPassword = passwordTxt.text;
      }else{
        if(confirmPasswordTxt.text==''){
          Tools.showToast("请二次确认支付密码");
          return;
        }
        confirmPassword = confirmPasswordTxt.text;
      }

      Network.editPassword(confirmPassword,passwordTxt.text,type).then((value){
        if(value){
          Tools.showToast("修改成功");
          step = 1;
          if (timer != null) {
            timer!.cancel();
            countdown = 0;
            countdownTxt = "获取验证码";
            buttonColor = ThemeConfig.themeColor;
          }
          Get.back();
        }
      });
    }
  }


  @override
  void onClose() {
    if (timer != null) {
      timer!.cancel();
    }
    super.onClose();
  }


}